/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package anton.admin.lunch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
 *
 * @author Joakim
 */

@ManagedBean
@SessionScoped

public class CreateLunch {
    
    @Resource(name="jdbc/antonsDatasource")
        private DataSource ds;
    
    private String lunchName;
    private String lunchDescription;
    private int lunchPrice;
    int i;

    /**
     * @return the lunchPrice
     */
    public int getLunchPrice() {
        return lunchPrice;
    }

    /**
     * @param lunchPrice the lunchPrice to set
     */
    public void setLunchPrice(int lunchPrice) {
        this.lunchPrice = lunchPrice;
    }

    /**
     * @return the lunchName
     */
    public String getLunchName() {
        return lunchName;
    }

    /**
     * @param lunchName the lunchName to set
     */
    public void setLunchName(String lunchName) {
        this.lunchName = lunchName;
    }

    /**
     * @return the lunchDescription
     */
    public String getLunchDescription() {
        return lunchDescription;
    }

    /**
     * @param lunchDescription the lunchDescription to set
     */
    public void setLunchDescription(String lunchDescription) {
        this.lunchDescription = lunchDescription;
    }
    
    
    public  String add() throws SQLException {       
        if(ds==null)
            throw new SQLException("Can't get data source");// throws exception i database not available
 
        //database connection
	Connection con = ds.getConnection();
                 
	if(con==null)
            throw new SQLException("Can't get database connection");
        try (PreparedStatement ps = con.prepareStatement(
            "insert into Lunch(lunch_name,lunch_description,lunch_price) values (?,?,?)" )){
            ps.setString( 1, lunchName );
            ps.setString(2, lunchDescription);
            ps.setInt(3, lunchPrice);
             
            i = ps.executeUpdate();   
         }      
         
         con.close();         
       
         return "createlunch";     
    } 
}
